/**
 * 
 */
package com.ygj.shop.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.base.common.datesource.MultipleDataSource;
import com.base.common.persistent.dao.SpecialShopMapper;
import com.base.common.persistent.model.SpecialShop;
import com.base.common.persistent.query.SpecialShopQuery;
import com.ygj.easemob.server.example.Main;
import org.springframework.stereotype.Service;

import com.base.common.persistent.dao.TShopsMapper;
import com.base.common.persistent.model.TShops;
import com.base.common.persistent.query.TShopsQuery;
import com.ygj.shop.service.ShopService;

import static jdk.nashorn.internal.objects.NativeString.substring;

/**
 * @author "kat.luhongkai"
 *
 */
@Service("shopService")
public class ShopServiceImpl implements ShopService {
	
	@Resource
	private TShopsMapper tShopsMapper;

	@Resource
	private SpecialShopMapper specialShopMapper;

	@Override
	public List<TShops> getShopsByCondition(TShopsQuery shopsQuery) {
		List<TShops> tShopses = tShopsMapper.selectObjectByCondition(shopsQuery);
        for (int i = 0; i < tShopses.size(); i++) {

            String logo = tShopses.get(i).getcLogo();
            if (logo != null && !"".equals(logo)) {
                String logoDir = logo.substring(0, 8);
                String newLogo = "http://120.25.120.99:2222/pic/" + logoDir.substring(0, 6) + "/" + logoDir.substring(6) + "/" + logo;
                tShopses.get(i).setcLogo(newLogo);
                tShopses.set(i, tShopses.get(i));
            }
        }
        return tShopses;
	}

	/* (non-Javadoc)
	 * @see com.ygj.shop.service.ShopService#getShopsByConditionCount(com.base.common.persistent.query.TShopsQuery)
	 */
	@Override
	public Long getShopsByConditionCount(TShopsQuery shopsQuery) {
		return tShopsMapper.selectObjectByConditionCount(shopsQuery);
	}

	@Override
	public List<TShops> getSpecialShopList(int categories) {
		List<TShops> tShopses = new ArrayList<>();
        SpecialShopQuery specialShopQuery = new SpecialShopQuery();
        specialShopQuery.setCategories(categories);
		List<SpecialShop> specialShops = specialShopMapper.selectByCondition(specialShopQuery);
        for (SpecialShop specialShop : specialShops) {

        }
        specialShops.forEach((item) -> {
			MultipleDataSource.setDataSourceKey("shopDataSource");
			TShops tShops = tShopsMapper.selectByPrimaryKey(Long.valueOf(item.getShopId()));
			String logo = tShops.getcLogo();
			if (logo != null && !"".equals(logo)) {
				String logoDir = logo.substring(0, 8);
				String newLogo = "http://120.25.120.99:2222/pic/" + logoDir.substring(0, 6) + "/" + logoDir.substring(6) + "/" + logo;
				tShops.setcLogo(newLogo);
			}
            tShopses.add(tShops);
			MultipleDataSource.setDataSourceKey("sqlserverDataSource");
		});

		return tShopses;
	}

    /*public static void main(String[] args){
        String str = "abcdefg";

        System.out.println(str.substring(4));
    }*/

}
